// 数据库类型定义和工具函数

// 获取数据库类型分组
export function getDataTypeGroups(databaseType) {
  const groups = {
    mysql: {
      '数值类型': [
        { label: 'TINYINT', value: 'tinyint', description: '1字节整数' },
        { label: 'SMALLINT', value: 'smallint', description: '2字节整数' },
        { label: 'MEDIUMINT', value: 'mediumint', description: '3字节整数' },
        { label: 'INT', value: 'int', description: '4字节整数' },
        { label: 'BIGINT', value: 'bigint', description: '8字节整数' },
        { label: 'DECIMAL', value: 'decimal', description: '精确小数' },
        { label: 'FLOAT', value: 'float', description: '单精度浮点' },
        { label: 'DOUBLE', value: 'double', description: '双精度浮点' }
      ],
      '字符串类型': [
        { label: 'CHAR', value: 'char', description: '定长字符串' },
        { label: 'VARCHAR', value: 'varchar', description: '变长字符串' },
        { label: 'TINYTEXT', value: 'tinytext', description: '255字符文本' },
        { label: 'TEXT', value: 'text', description: '64KB文本' },
        { label: 'MEDIUMTEXT', value: 'mediumtext', description: '16MB文本' },
        { label: 'LONGTEXT', value: 'longtext', description: '4GB文本' }
      ],
      '日期时间类型': [
        { label: 'DATE', value: 'date', description: '日期' },
        { label: 'TIME', value: 'time', description: '时间' },
        { label: 'DATETIME', value: 'datetime', description: '日期时间' },
        { label: 'TIMESTAMP', value: 'timestamp', description: '时间戳' },
        { label: 'YEAR', value: 'year', description: '年份' }
      ],
      '二进制类型': [
        { label: 'BINARY', value: 'binary', description: '定长二进制' },
        { label: 'VARBINARY', value: 'varbinary', description: '变长二进制' },
        { label: 'TINYBLOB', value: 'tinyblob', description: '255字节' },
        { label: 'BLOB', value: 'blob', description: '64KB' },
        { label: 'MEDIUMBLOB', value: 'mediumblob', description: '16MB' },
        { label: 'LONGBLOB', value: 'longblob', description: '4GB' }
      ],
      '其他类型': [
        { label: 'JSON', value: 'json', description: 'JSON文档' },
        { label: 'ENUM', value: 'enum', description: '枚举类型' },
        { label: 'SET', value: 'set', description: '集合类型' },
        { label: 'BOOLEAN', value: 'boolean', description: '布尔值' },
        { label: 'BIT', value: 'bit', description: '位字段' }
      ]
    },
    postgresql: {
      '数值类型': [
        { label: 'SMALLINT', value: 'smallint', description: '2字节整数' },
        { label: 'INTEGER', value: 'integer', description: '4字节整数' },
        { label: 'BIGINT', value: 'bigint', description: '8字节整数' },
        { label: 'DECIMAL', value: 'decimal', description: '精确小数' },
        { label: 'NUMERIC', value: 'numeric', description: '精确小数' },
        { label: 'REAL', value: 'real', description: '单精度浮点' },
        { label: 'DOUBLE PRECISION', value: 'double precision', description: '双精度浮点' }
      ],
      '自增类型': [
        { label: 'SERIAL', value: 'serial', description: '4字节自增' },
        { label: 'SMALLSERIAL', value: 'smallserial', description: '2字节自增' },
        { label: 'BIGSERIAL', value: 'bigserial', description: '8字节自增' }
      ],
      '字符类型': [
        { label: 'CHARACTER', value: 'character', description: '定长字符串' },
        { label: 'VARCHAR', value: 'varchar', description: '变长字符串' },
        { label: 'TEXT', value: 'text', description: '不限长文本' }
      ],
      '日期时间类型': [
        { label: 'DATE', value: 'date', description: '日期' },
        { label: 'TIME', value: 'time', description: '时间' },
        { label: 'TIMESTAMP', value: 'timestamp', description: '时间戳' },
        { label: 'INTERVAL', value: 'interval', description: '时间间隔' }
      ],
      '布尔类型': [
        { label: 'BOOLEAN', value: 'boolean', description: '布尔值' }
      ],
      '二进制类型': [
        { label: 'BYTEA', value: 'bytea', description: '二进制数据' }
      ],
      '其他类型': [
        { label: 'UUID', value: 'uuid', description: 'UUID' },
        { label: 'JSON', value: 'json', description: 'JSON文档' },
        { label: 'JSONB', value: 'jsonb', description: '二进制JSON' },
        { label: 'ARRAY', value: 'array', description: '数组' },
        { label: 'MONEY', value: 'money', description: '货币' }
      ]
    },
    sqlite: {
      '基本类型': [
        { label: 'INTEGER', value: 'integer', description: '整数' },
        { label: 'REAL', value: 'real', description: '浮点数' },
        { label: 'TEXT', value: 'text', description: '文本' },
        { label: 'BLOB', value: 'blob', description: '二进制' },
        { label: 'NUMERIC', value: 'numeric', description: '数值' }
      ],
      '扩展类型': [
        { label: 'VARCHAR', value: 'varchar', description: '变长字符串' },
        { label: 'CHAR', value: 'char', description: '定长字符串' },
        { label: 'DATE', value: 'date', description: '日期' },
        { label: 'DATETIME', value: 'datetime', description: '日期时间' },
        { label: 'BOOLEAN', value: 'boolean', description: '布尔值' }
      ]
    },
    sqlserver: {
      '精确数值': [
        { label: 'BIT', value: 'bit', description: '0或1' },
        { label: 'TINYINT', value: 'tinyint', description: '1字节整数' },
        { label: 'SMALLINT', value: 'smallint', description: '2字节整数' },
        { label: 'INT', value: 'int', description: '4字节整数' },
        { label: 'BIGINT', value: 'bigint', description: '8字节整数' },
        { label: 'DECIMAL', value: 'decimal', description: '定点数' },
        { label: 'NUMERIC', value: 'numeric', description: '定点数' },
        { label: 'MONEY', value: 'money', description: '货币' },
        { label: 'SMALLMONEY', value: 'smallmoney', description: '小货币' }
      ],
      '近似数值': [
        { label: 'FLOAT', value: 'float', description: '浮点数' },
        { label: 'REAL', value: 'real', description: '单精度浮点' }
      ],
      '字符串': [
        { label: 'CHAR', value: 'char', description: '定长字符串' },
        { label: 'VARCHAR', value: 'varchar', description: '变长字符串' },
        { label: 'TEXT', value: 'text', description: '大文本' },
        { label: 'NCHAR', value: 'nchar', description: 'Unicode定长' },
        { label: 'NVARCHAR', value: 'nvarchar', description: 'Unicode变长' },
        { label: 'NTEXT', value: 'ntext', description: 'Unicode大文本' }
      ],
      '日期时间': [
        { label: 'DATE', value: 'date', description: '日期' },
        { label: 'TIME', value: 'time', description: '时间' },
        { label: 'DATETIME', value: 'datetime', description: '日期时间' },
        { label: 'DATETIME2', value: 'datetime2', description: '高精度日期时间' },
        { label: 'SMALLDATETIME', value: 'smalldatetime', description: '小日期时间' },
        { label: 'DATETIMEOFFSET', value: 'datetimeoffset', description: '带时区' }
      ],
      '二进制': [
        { label: 'BINARY', value: 'binary', description: '定长二进制' },
        { label: 'VARBINARY', value: 'varbinary', description: '变长二进制' },
        { label: 'IMAGE', value: 'image', description: '图像' }
      ],
      '其他': [
        { label: 'XML', value: 'xml', description: 'XML文档' },
        { label: 'JSON', value: 'json', description: 'JSON文档' },
        { label: 'UNIQUEIDENTIFIER', value: 'uniqueidentifier', description: 'GUID' }
      ]
    },
    dameng: {
      '数值类型': [
        { label: 'TINYINT', value: 'tinyint', description: '1字节整数' },
        { label: 'SMALLINT', value: 'smallint', description: '2字节整数' },
        { label: 'INT', value: 'int', description: '4字节整数' },
        { label: 'INTEGER', value: 'integer', description: '4字节整数' },
        { label: 'BIGINT', value: 'bigint', description: '8字节整数' },
        { label: 'NUMBER', value: 'number', description: '可变精度数值' },
        { label: 'DECIMAL', value: 'decimal', description: '定点数' },
        { label: 'FLOAT', value: 'float', description: '浮点数' },
        { label: 'DOUBLE', value: 'double', description: '双精度浮点' }
      ],
      '字符类型': [
        { label: 'CHAR', value: 'char', description: '定长字符串' },
        { label: 'VARCHAR', value: 'varchar', description: '变长字符串' },
        { label: 'VARCHAR2', value: 'varchar2', description: '变长字符串2' },
        { label: 'TEXT', value: 'text', description: '文本' },
        { label: 'CLOB', value: 'clob', description: '字符大对象' }
      ],
      '日期时间类型': [
        { label: 'DATE', value: 'date', description: '日期' },
        { label: 'TIME', value: 'time', description: '时间' },
        { label: 'TIMESTAMP', value: 'timestamp', description: '时间戳' },
        { label: 'DATETIME', value: 'datetime', description: '日期时间' }
      ],
      '二进制类型': [
        { label: 'BINARY', value: 'binary', description: '定长二进制' },
        { label: 'VARBINARY', value: 'varbinary', description: '变长二进制' },
        { label: 'BLOB', value: 'blob', description: '二进制大对象' },
        { label: 'IMAGE', value: 'image', description: '图像' }
      ],
      '其他类型': [
        { label: 'BOOLEAN', value: 'boolean', description: '布尔值' },
        { label: 'BIT', value: 'bit', description: '位' }
      ]
    }
  }

  return groups[databaseType] || groups.mysql
}

// 获取类型信息
export function getTypeInfo(type, databaseType) {
  const typeInfoMap = {
    mysql: {
      'varchar': { defaultLength: 255, needsLength: true },
      'char': { defaultLength: 1, needsLength: true },
      'int': { defaultLength: 11 },
      'tinyint': { defaultLength: 4 },
      'smallint': { defaultLength: 6 },
      'mediumint': { defaultLength: 9 },
      'bigint': { defaultLength: 20 },
      'decimal': { defaultLength: '10,2', needsPrecision: true },
      'float': { needsPrecision: true },
      'double': { needsPrecision: true },
      'enum': { needsValues: true },
      'set': { needsValues: true }
    },
    postgresql: {
      'varchar': { needsLength: true, defaultLength: 255 },
      'character': { needsLength: true, defaultLength: 1 },
      'numeric': { needsPrecision: true, defaultLength: '10,2' },
      'decimal': { needsPrecision: true, defaultLength: '10,2' }
    },
    // ... 其他数据库类型
  }

  const dbTypeInfo = typeInfoMap[databaseType] || typeInfoMap.mysql
  return dbTypeInfo[type.toLowerCase()] || {}
}

// 获取数据类型的分类
export function getTypeCategory(type) {
  const categories = {
    numeric: ['int', 'integer', 'bigint', 'smallint', 'tinyint', 'mediumint', 'decimal', 'numeric', 'float', 'double', 'real', 'money', 'smallmoney'],
    string: ['varchar', 'char', 'text', 'tinytext', 'mediumtext', 'longtext', 'nvarchar', 'nchar', 'ntext'],
    datetime: ['date', 'time', 'datetime', 'timestamp', 'year', 'interval'],
    binary: ['binary', 'varbinary', 'blob', 'tinyblob', 'mediumblob', 'longblob', 'bytea', 'image'],
    boolean: ['boolean', 'bool', 'bit'],
    json: ['json', 'jsonb'],
    other: ['enum', 'set', 'uuid', 'xml', 'array']
  }

  const lowerType = type.toLowerCase()
  for (const [category, types] of Object.entries(categories)) {
    if (types.includes(lowerType)) {
      return category
    }
  }
  return 'other'
}

// 检查类型是否需要长度
export function needsLength(type) {
  const typesWithLength = [
    'varchar', 'char', 'nvarchar', 'nchar',
    'binary', 'varbinary',
    'bit'
  ]
  return typesWithLength.includes(type.toLowerCase())
}

// 检查类型是否需要精度
export function needsPrecision(type) {
  const typesWithPrecision = [
    'decimal', 'numeric', 'float', 'double', 'real', 'number'
  ]
  return typesWithPrecision.includes(type.toLowerCase())
}

// 检查类型是否可以自增
export function canAutoIncrement(type) {
  const autoIncrementTypes = [
    'int', 'integer', 'bigint', 'smallint', 'tinyint', 'mediumint',
    'serial', 'bigserial', 'smallserial'
  ]
  return autoIncrementTypes.includes(type.toLowerCase())
}

// 检查是否是字符串类型
export function isStringType(type) {
  const stringTypes = [
    'varchar', 'char', 'text', 'tinytext', 'mediumtext', 'longtext',
    'nvarchar', 'nchar', 'ntext', 'clob', 'varchar2'
  ]
  return stringTypes.includes(type.toLowerCase())
}

// 检查是否是数值类型
export function isNumericType(type) {
  return getTypeCategory(type) === 'numeric'
}

// 检查是否是日期时间类型
export function isDateTimeType(type) {
  return getTypeCategory(type) === 'datetime'
}

// 获取默认值建议
export function getDefaultValueSuggestions(type, databaseType) {
  const category = getTypeCategory(type)
  const suggestions = []

  // 通用建议
  suggestions.push({ label: 'NULL', value: 'NULL' })

  switch (category) {
    case 'numeric':
      suggestions.push(
        { label: '0', value: '0' },
        { label: '1', value: '1' },
        { label: '-1', value: '-1' }
      )
      break
    case 'string':
      suggestions.push(
        { label: '空字符串', value: '' },
        { label: '默认', value: 'default' }
      )
      break
    case 'datetime':
      if (databaseType === 'mysql') {
        suggestions.push(
          { label: '当前时间戳', value: 'CURRENT_TIMESTAMP' },
          { label: '0000-00-00', value: '0000-00-00' }
        )
        if (type.toLowerCase() === 'timestamp') {
          suggestions.push(
            { label: '更新时间戳', value: 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' }
          )
        }
      } else if (databaseType === 'postgresql') {
        suggestions.push(
          { label: '当前时间', value: 'NOW()' },
          { label: '当前日期', value: 'CURRENT_DATE' }
        )
      }
      break
    case 'boolean':
      suggestions.push(
        { label: 'TRUE', value: 'TRUE' },
        { label: 'FALSE', value: 'FALSE' },
        { label: '1', value: '1' },
        { label: '0', value: '0' }
      )
      break
    case 'json':
      suggestions.push(
        { label: '空对象', value: '{}' },
        { label: '空数组', value: '[]' }
      )
      break
  }

  return suggestions
} 